package com.ruoyi.project.oss;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

@Controller
@RequestMapping("/oss")
@Slf4j
public class UploadController {
    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;
    @Autowired
    private ConstantProperties constantProperties;



    @GetMapping("/toUploadBlog")
    public String toUploadBlog() {
        return "oss/upload";
    }


    @PostMapping("/toUploadBlog")
    @ResponseBody
    public AjaxResult toUploadBlogPost(MultipartFile file) {
        log.info("=========>文件上传");
        String uploadUrl = "";
        try {

            if (null != file) {
                String filename = file.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    //上传到OSS
                     uploadUrl = aliyunOSSUtil.upload(newFile);
                    log.info(uploadUrl);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        return "oss/index";
        return  AjaxResult.success(uploadUrl);
    }


    @PostMapping("/tinymceUpload")
    @ResponseBody
    public JSONObject tinymceUpload(MultipartFile file) {
        log.info("=========>tinymceUpload文件上传");
        String uploadUrl = "";
        try {

            if (null != file) {
                String filename = file.getOriginalFilename();
                if (!"".equals(filename.trim())) {
                    File newFile = new File(filename);
                    FileOutputStream os = new FileOutputStream(newFile);
                    os.write(file.getBytes());
                    os.close();
                    file.transferTo(newFile);
                    //上传到OSS
                    uploadUrl = aliyunOSSUtil.upload(newFile);
                    log.info(uploadUrl);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
//        return "oss/index";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("location",uploadUrl);
        return  jsonObject;
    }




    @GetMapping("/getObjectList")
    @ResponseBody
    public List<String> getObjectList() {
        String bucketName = constantProperties.getBucketname();
        System.out.println(bucketName);
        List<String> objectList = aliyunOSSUtil.getObjectList(bucketName);
        return objectList;
    }

    @GetMapping("/delete")
    @ResponseBody
    public String deleteBlog(@RequestParam("key")String key){
        aliyunOSSUtil.deleteBlog(key);
        return "删除成功";
    }
}

